package com.anda.jsu.yangsp.core.sys.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.anda.jsu.yangsp.core.sys.logger.AndaLogger;
import com.anda.jsu.yangsp.core.sys.mail.bean.AndaAuthenticatorBean;
import com.anda.jsu.yangsp.core.sys.mail.bean.AndaMailSenderInfoBean;

/**
 * 邮件发送
 * 
 * @author xiaolu
 * @since 2012-12-06
 * 
 */
public class AndaSimpleMailSender {

	/**
	 * 以文本格式发送邮件
	 * 
	 * @param mailInfo
	 *            待发送邮件信息
	 * @return
	 * @throws MessagingException
	 */
	public static boolean sendMailAsText(AndaMailSenderInfoBean mailInfo)
			throws MessagingException {
		AndaAuthenticatorBean authenticator = null;
		Properties prop = mailInfo.getProperties();
		// 判断是否需要身份验证
		if (mailInfo.isValidate()) {
			// 如果需要身份认证,则创建一个密码验证器
			authenticator = new AndaAuthenticatorBean(mailInfo.getUserName(),
					mailInfo.getSecurityWord());
		}
		// 根据会话属性和密码验证器构造一个发送邮件的Session
		Session sendMailSession = Session.getDefaultInstance(prop,
				authenticator);
		// 根据Session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		// 创建邮件发送者地址
		Address fromMailAddress = new InternetAddress(mailInfo
				.getFromMailAddress());
		// 设置邮件消息的发送者
		mailMessage.setFrom(fromMailAddress);
		// 创建邮件接收者地址,并设置到邮件消息中
		Address toMailAddress = new InternetAddress(mailInfo.getToMailAddress());
		mailMessage.setRecipient(Message.RecipientType.TO, toMailAddress);
		// 设置邮件消息主题
		mailMessage.setSubject(mailInfo.getMailSubject());
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());
		// 设置邮件消息的主要内容
		mailMessage.setText(mailInfo.getMailContent());
		// 发送邮件
		Transport.send(mailMessage);
		return true;
	}

	/**
	 * 以HTML格式发送邮件
	 * 
	 * @param mailInfo
	 *            待发送邮件信息
	 * @return
	 */
	public static boolean sendMailAsHtml(AndaMailSenderInfoBean mailInfo) {
		// 判断是否需要验证
		AndaAuthenticatorBean authenticator = null;
		Properties prop = mailInfo.getProperties();
		if (mailInfo.isValidate()) {
			// 创建密码验证器
			authenticator = new AndaAuthenticatorBean(mailInfo.getUserName(),
					mailInfo.getSecurityWord());
		}
		// 根据邮件会话属性和密码验证器构造发送邮件的Session
		Session sendMailSession = Session.getDefaultInstance(prop,
				authenticator);
		try {
			// 根据会话Session创建邮件消息
			Message mailMessage = new MimeMessage(sendMailSession);
			// 创建邮件发送者地址,并设置到邮件消息中
			Address fromMailAddress = new InternetAddress(mailInfo
					.getFromMailAddress());
			mailMessage.setFrom(fromMailAddress);
			// 创建邮件消息接收者地址,并设置到邮件消息中
			Address toMailAddress = new InternetAddress(mailInfo
					.getToMailAddress());
			mailMessage.setRecipient(Message.RecipientType.TO, toMailAddress);
			// 设置邮件消息的主题
			mailMessage.setSubject(mailInfo.getMailSubject());
			// 设置消息发送日期
			mailMessage.setSentDate(new Date());
			// MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象
			Multipart mailPart = new MimeMultipart();
			// 创建一个包含HTML内容的MimeBodyPart
			BodyPart htmlBodyPart = new MimeBodyPart();
			// 设置HTML内容
			htmlBodyPart.setContent(mailInfo.getMailContent(),
					"text/html; charset=utf-8");
			mailPart.addBodyPart(htmlBodyPart);
			// 将MimeMultipart对象设置为邮件内容
			mailMessage.setContent(mailPart);
			// 发送邮件
			Transport.send(mailMessage);
		} catch (Exception e) {
			AndaLogger.recordLogger(e.getMessage());
			return false;
		}
		return true;
	}

}
